<template>
  <div>
    <!-- 导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>


    <el-card>

      <!-- 搜索框 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="queryinfo.query" clearable @clear="clearsearch">
            <el-button slot="append" icon="el-icon-search" @click="searchbtns"> </el-button>
          </el-input>
        </el-col>
        <!-- 搜索框按钮 -->
        <el-button type="primary" @click="addGoodsPin">添加商品</el-button>
      </el-row>

      <!-- 商品列表 -->
      <el-table :data="goodsList" border>
        <el-table-column type="index" label="#" width="60"> </el-table-column>
        <el-table-column prop="goods_name" label="商品名称" width="450"> </el-table-column>
        <el-table-column prop="goods_price" label="商品价格(元)" width="110"> </el-table-column>
        <el-table-column prop="goods_number" label="商品数量" width="80"> </el-table-column>
        <el-table-column prop="add_time" label="创建时间" width="160"> 
          <template slot-scope="scope">
            {{scope.row.add_time | dateFormat}}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="370">
          <template slot-scope="scope">
            <el-button type="primary" size="small" icon="el-icon-edit">编辑</el-button>
            <el-button type="danger" size="small" icon="el-icon-delete" @click="deleteGoods(scope.row.goods_id)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页功能 -->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page="queryinfo.pagenum" :page-sizes="[3, 7, 10]" :page-size="queryinfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>



    </el-card>
  </div>
</template>


<script>
  export default {
    data() {
      return {
        //  get请求要带一个这样的对象，把请求的数据传递过去
        queryinfo: {
          query: '',
          pagenum: 1,
          pagesize: 7,
        },
        goodsList: [],
        total: 0,
      }
    },

    created() {
      this.getGoodsList()
    },

    methods: {
      async getGoodsList() {
        const {
          data: res
        } = await this.$http.get('/goods', {
          params: this.queryinfo
        })

        this.goodsList = res.data.goods
        this.total = res.data.total
      },

      // 分页符
      // 传递过来一个参数，赋值给自己定义的对象参数，传递给服务器，再重新调用一下列表刷新，如下一样
      handleSizeChange(newsize) {
        this.queryinfo.pagesize = newsize
        this.getGoodsList()
      },

      // 传递过来一个参数，赋值给自己定义的对象参数，传递给服务器，再重新调用一下列表刷新，如下一样
      handleCurrentChange(newpag) {
        this.queryinfo.pagenum = newpag
        this.getGoodsList()
      },

      // 搜索框按钮搜索
      searchbtns() {
        this.getGoodsList()
      },
      // 搜索完之后重新要重新加载到初始页面
      clearsearch() {
        this.getGoodsList()
      },
      // 删除商品条
      async deleteGoods(id) {
        const confrimcon = await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).catch(err => err)

        if (confrimcon !== 'confirm') {
          return this.$message.info('取消删除成功')
        }
        const {
          data: res
        } = await this.$http.delete(`goods/${id}`)
        if (res.meta.status !== 200) {
          return this.$message.error('删除失败')
        }
        this.$message.success('删除成功')
      },

      // 点击添加商品按钮进项跳转
      addGoodsPin() {
        this.$router.push('/goods/add')
      }
    }
  }
</script>


<style lang="less" scoped>

</style>